package com.wokeMy.view.channe.ronghui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.html.RhkjzfHtmlActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class RhKjzfActivity extends BaseActivity implements View.OnClickListener{
    ImageView back_rhkjzf_iv;
    RelativeLayout zfcard_rhkjzf_rl;
    Button zhifu_rhkjzf_bt;
    TextView khk_rhkjzf_tv,hqyzm_rhkjzf_tv;
    EditText yzm_rhkjzf_et;

    Data_bindcarlist listdata;
    UnionPayChannel unionPayChannel;
    String pay_card_no,bankCode;
    String user_id;
    int way_id;
    int payWay;
    String useName,merchantName,merchantBillName,legalPersonID,merchantPersonPhone;
    float amount;
    String orderNum;
    String msgCode;

    Dialog dialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_rh_kjzf);
    }

    @Override
    public void initActivity() {
        back_rhkjzf_iv.setOnClickListener(this);
        zfcard_rhkjzf_rl.setOnClickListener(this);
        zhifu_rhkjzf_bt.setOnClickListener(this);
        hqyzm_rhkjzf_tv.setOnClickListener(this);
    }

    @Override
    public void initView() {

        back_rhkjzf_iv = myFindViewById(R.id.back_rhkjzf_iv);
        zfcard_rhkjzf_rl = myFindViewById(R.id.zfcard_rhkjzf_rl);
        khk_rhkjzf_tv = myFindViewById(R.id.khk_rhkjzf_tv);
        zhifu_rhkjzf_bt = myFindViewById(R.id.zhifu_rhkjzf_bt);
        yzm_rhkjzf_et = myFindViewById(R.id.yzm_rhkjzf_et);
        hqyzm_rhkjzf_tv = myFindViewById(R.id.hqyzm_rhkjzf_tv);
    }

    @Override
    public void initData() {
        MyApp myApp=(MyApp)getApplication();
        Intent intent=getIntent();
        Datas_load datas_load=myApp.getDatas_load();
        user_id=datas_load.getId();

        unionPayChannel = (UnionPayChannel)(intent.getSerializableExtra("UnionPayChannel"));
        amount=intent.getFloatExtra ("money", 0.01f);
        Log.e("unionPayChannel",unionPayChannel.toString());
        way_id=unionPayChannel.getWay();
        payWay=unionPayChannel.getKind();
        String name="名字为空";
        if (!datas_load.getCom_name().equals("")){
            name=datas_load.getCom_name();
        }else {
            name=datas_load.getUser_nicename();
        }
        useName=name;
        merchantName=name+"的商户";
        merchantBillName=name+"的商户";
        legalPersonID=datas_load.getUser_no();
        merchantPersonPhone=datas_load.getUser_login();
        openRhGood();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_rhkjzf_iv:
                finish();
                break;
            case R.id.hqyzm_rhkjzf_tv:
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(RhKjzfActivity.this,"请选择银行卡",Toast.LENGTH_SHORT).show();
                }else {
                    if (!fastClick()){
                        hqYzm();
                    }
                }

                break;
            case R.id.zfcard_rhkjzf_rl:
                Intent intent = new Intent (RhKjzfActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "rhkjzf");
                startActivityForResult (intent, Constant.REQUEST_RHKJZF);
                break;
            case R.id.zhifu_rhkjzf_bt:
                    msgCode=yzm_rhkjzf_et.getText().toString().trim();
                    if (pay_card_no==null||pay_card_no.equals("")){
                        Toast.makeText(this,"请选择支付卡",Toast.LENGTH_SHORT).show();
                    }else if(msgCode==null||msgCode.equals("")) {
                        Toast.makeText(this,"请填写验证码",Toast.LENGTH_SHORT).show();
                    }else{
                        if (!fastClick()) {
                            dialog = Util.createLoadingDialog(RhKjzfActivity.this);
                            rhZhifu();
                        }
                    }

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==Constant.RESPONSE_RHKJZF){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=listdata.cardno;
            khk_rhkjzf_tv.setText(pay_card_no);
            isKaitongZhifu();
        }
    }
    //判断是否开同快捷支付
    public void isKaitongZhifu(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("accNo",pay_card_no);
        Log.e("判断是否开同快捷支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.RHKJZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("判断是否开同快捷支付", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        if (jsonObject.getString("flag").equals("success")){
                            JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                            //看result 0未开通 1开通T0 2 开通T1 3开通 T1,T0 都开同
                            String result1=jsonObject1.getString("result");
                            if (result1.equals("0")){
                                AlertDialog alertDialog;
                                alertDialog = new AlertDialog.Builder(RhKjzfActivity.this).
                                        setTitle("该卡尚未开通快捷支付").
                                        setPositiveButton("去开通", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                OpenRhkjzf();
                                            }
                                        }).
                                        setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();
                                            }
                                        }).create();
                                alertDialog.show();
                            }else {
                                Toast.makeText(RhKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                            }
                        }else {
                            Toast.makeText(RhKjzfActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                try {
                    String result=new String(bytes, "utf-8");
                    Log.e("onFailure",result );
                    Toast.makeText (RhKjzfActivity.this, "请求失败", Toast.LENGTH_SHORT)
                            .show ();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
        });
    }
    //开通融汇快捷支付
    public void OpenRhkjzf(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("accNo",pay_card_no);
        Log.e("//开通融汇快捷支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.OPENRHKJZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("开通融汇快捷支付", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        if (jsonObject.getString("flag").equals("success")) {
                             String rhkjzfhtml = jsonObject.getJSONObject("data").getString("data");
                            Intent intent =new Intent(RhKjzfActivity.this, RhkjzfHtmlActivity.class);
                            intent.putExtra("rhkjzfhtml",rhkjzfhtml);
                            startActivity(intent);
                        }else{
                            Toast.makeText(RhKjzfActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });

    }
    // 获取北京融汇验证码
    public void  hqYzm(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("amount",amount);
        params.put("goodsName",getResources().getString(R.string.oem_app_name)+"的产品");
        params.put("orderIp",Util.getIPAddress(this));
        params.put("cardNo",pay_card_no);
        //        params.put("creditCardNo","6225757545273378");

        Log.e("获取北京融汇验证码",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.HQYZM_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("获取北京融汇验证码///", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                        if (jsonObject1.getString("respcode").equals("0000")){
                            orderNum=jsonObject1.getString("ordernum");
                            Toast.makeText(RhKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(RhKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
    // 北京融汇商户产品开通
    public void  openRhGood(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("productType","KUAI");
        //开通无卡快捷支付
        //        params.put("creditCardNo","6225757545273378");

        Log.e("北京融汇商户产品开通",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.OPENGOOD_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("北京融汇商户产品开通///", result);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
    //北京融汇快捷支付填写完验证码后确定支付
    public void rhZhifu(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("amount",amount);
        params.put("orderNum",orderNum);
        params.put("smsVerifyCode",msgCode);
        //北京融汇快捷支付填写完验证码后确定支付
        //        params.put("creditCardNo","6225757545273378");
        Log.e("北京融汇快捷支付码后确定支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.RHQRZHIFU_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("北京融汇快捷支付填写完验证码后确定支付", result);
                    JSONObject  jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                        String respcode=jsonObject1.getString("respcode");
                        if (respcode.equals("0000")){
                            Toast.makeText(RhKjzfActivity.this,"支付成功",Toast.LENGTH_LONG).show();
                            finish();
                        }else {
                            Toast.makeText(RhKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }




                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });

    }
    public void rhlz(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("amount",amount);
        params.put("payWay",payWay);
        params.put("merchantName",merchantName);
        params.put("merchantBillName",merchantBillName);
        params.put("installProvince","福建省");
        params.put("installCity","厦门市");
        params.put("installCounty","思明区");
        params.put("operateAddress","福建省厦门市思明区虎园路2号");
        params.put("merchantType","PERSON");
        params.put("legalPersonName",useName);
        params.put("legalPersonID",legalPersonID);
        params.put("merchantPersonName",useName);
        params.put("merchantPersonPhone",merchantPersonPhone);
        params.put("bankType","TOPRIVATE");
        params.put("accountName",useName);
        params.put("accountNo",pay_card_no);
        params.put("bankCode",bankCode);
        //        params.put("creditCardNo","6225757545273378");

        Log.e("params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.RHSHLZ_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("YINLZCT_URL/////////", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                        String respcode=jsonObject1.getString("respcode");
                        if (respcode.equals("0000")){
                            Toast.makeText(RhKjzfActivity.this,"入驻成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(RhKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
}
